﻿using System.Collections.Generic;
using System.IO;
using System.Web.Hosting;
using System.Xml;

namespace visinia.SmartEngine.Installer
{
    internal class ContentFileParser
    {
        public static List<ContentFileInfo> ParseContentFile(string ContentFile)
        {
            List<ContentFileInfo> ContentFileList = new List<ContentFileInfo>();
            XmlDocument doc = new XmlDocument();
            doc.Load(ContentFile);
            XmlNode visiniaNode = doc.DocumentElement;
            if (visiniaNode != null)
            {
                // TODO check visinia version 

                // this is content data
                XmlNode contentNodes = visiniaNode["content"];
                if (contentNodes != null)
                {
                    foreach (XmlNode contentNode in contentNodes.ChildNodes)
                    {
                        string nodeType = contentNode.Name;
                        if (string.Compare(nodeType, "module", true) == 0)
                        {
                            // the content file has following module(s)
                            XmlNode nameNode = contentNode["name"];
                            if (nameNode == null)
                                continue;
                            if (string.IsNullOrEmpty(nameNode.InnerText))
                                continue;
                            // put the module to the General package if not specified
                            string modulePackage = "General";
                            XmlNode packageNode = contentNode["package"];
                            if (packageNode != null)
                            {
                                if (!string.IsNullOrEmpty(packageNode.InnerText))
                                    modulePackage = packageNode.InnerText;
                            }

                            ContentFileInfo content = new ContentFileInfo();
                            content.Name = nameNode.InnerText;
                            content.Type = ContentFileType.Module;
                            content.Package = modulePackage;
                            // is this module already installed
                            content.IsInstalled = ModulesInstalledController.GetInstalledModules().Exists(
                                p => (string.Compare(p.Name, content.Name, true) == 0) &&
                                    (string.Compare(p.Package, content.Package, true) == 0));

                            // remove .content from content file name the remaining part is
                            //just the zip file actually containing the contents
                            content.ZipFileName = Path.GetFileName(ContentFile.Remove(ContentFile.Length - 8));
                            ContentFileList.Add(content);
                        }
                        if (string.Compare(nodeType, "plugin", true) == 0)
                        {
                            // the content file has following plugin(s)
                            XmlNode nameNode = contentNode["name"];
                            if (nameNode == null)
                                continue;
                            if (string.IsNullOrEmpty(nameNode.InnerText))
                                continue;
                            ContentFileInfo content = new ContentFileInfo();
                            content.Name = nameNode.InnerText;
                            content.Type = ContentFileType.Plugin;
                            // is this plugin already installed
                            content.IsInstalled = PluginController.GetInstalledPlugins().Exists(
                                p => (string.Compare(p.Name, content.Name, true) == 0));

                            // remove .content from content file name the remaining part is
                            //just the zip file actually containing the contents
                            content.ZipFileName = Path.GetFileName(ContentFile.Remove(ContentFile.Length - 8));                            
                            ContentFileList.Add(content);
                        }
                        if (string.Compare(nodeType, "theme", true) == 0)
                        {
                            // the content file has following theme(s)
                            XmlNode nameNode = contentNode["name"];
                            if (nameNode == null)
                                continue;
                            if (string.IsNullOrEmpty(nameNode.InnerText))
                                continue;
                            ContentFileInfo content = new ContentFileInfo();
                            content.Name = nameNode.InnerText;
                            content.Type = ContentFileType.Theme;
                            // is this theme already installed                            
                            content.IsInstalled = File.Exists(HostingEnvironment.MapPath(WebRoots.GetThemesRoot + content.Name + "\\page.master"));

                            // remove .content from content file name the remaining part is
                            //just the zip file actually containing the contents
                            content.ZipFileName = Path.GetFileName(ContentFile.Remove(ContentFile.Length - 8));
                            ContentFileList.Add(content);
                        }
                    }                    
                }
            }
            return ContentFileList;
        }
    }    
}
